﻿using System;
using UnityEngine;
using UnityEngine.UI;


public class SkinsItem : MonoBehaviour
{
    public int Id;

    public Image icon;

    public GameObject lockImg;
    public Text priceText;

    Action<SkinsItem> m_OnClick;
    Action m_OnConfirm;
    bool m_Unlock;
    public GameObject selectSign;

    public bool unlock => m_Unlock;

    internal void Init(SkinVCData skin, Action<SkinsItem> onSelect, Action onConfirm)
    {
        this.Id = skin.skinId; 
        m_OnClick = onSelect;
        m_OnConfirm = onConfirm;
        m_Unlock = skin.unlock;

        if (priceText)
        {
            priceText.text = skin.price.ToString();

            if(skin.price <= 0)
            {
                priceText.transform.parent.gameObject.SetActive(false);
            }
        }

        if (lockImg)
            lockImg.SetActive(!skin.unlock);

        var iconSpr = GameResourceManager.LoadSkinIcon(skin.skinId);
        icon.sprite = iconSpr;
    }

    public void OnClick()
    {
        m_OnClick?.Invoke(this);
    }

    public void OnClickConfirm()
    {
        m_OnConfirm?.Invoke();
    }

    internal void UpdateSkin(SkinVCData skin)
    {
        if (lockImg)
            lockImg.SetActive(!skin.unlock);

        priceText.transform.parent.gameObject.SetActive(false);

    }
}
